import { _decorator, Node, ProgressBar } from 'cc';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { LayerType } from '../../Basic/Config/BaseEnum';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType, ShareEvent } from '../Config/GameEnum';
import { GameParams } from '../../Basic/Config/DataConfig';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { EnergyModel } from './EnergyModel';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
const { ccclass, property } = _decorator;

@ccclass('EnergyPanel')
export class EnergyPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;
  @property(Node)
  baseNode: Node;

  @property(ProgressBar)
  bar: ProgressBar;

  @property(Node)
  btn_share: Node;
  @property(Node)
  btn_ad: Node;
  @property(Node)
  btn_close: Node;
  onContinue() {
    console.log('点击继续');
  }

  protected onLoad(): void {
    // super.onLoad();
    EventManager.Ins().register(EventType.AD_ENERGY, this.onAd.bind(this), this);
    EventManager.Ins().register(EventType.SHARE_ENERGY, this.onShare.bind(this), this);
  }

  protected onEnable(): void {
    super.onEnable();
    this.bar.progress = DataMgr.gameParams.get(GameParams.energy_award) / 2 >= 1 ? 1 : DataMgr.gameParams.get(GameParams.energy_award) / 2;
    this.btn_share.active = DataMgr.gameParams.get(GameParams.energy_award) <= 0;
    this.btn_ad.active = DataMgr.gameParams.get(GameParams.energy_award) > 0 && DataMgr.gameParams.get(GameParams.energy_award) <= 2;
    this.btn_close.active = !this.btn_share.active && !this.btn_ad.active;
    // 测试
    // GameManager.Ins().player.addPlayParam(GameParams.PHYSICS, 0, true);
    // GameManager.Ins().player.addPlayParam(GameParams.PHYSICS_TIME, 10 * 60, true);
    // G_Config.SaveData();
    // EventManager.Ins().emit(EventType.UPDATE_HEAD_BAR);
  }

  onClickClose() {
    // EventManager.Ins().emit(EventType.CLICK_SOUND_2);
    // EnergyMode.Ins().hide();
    this.clickClose();
  }

  onShare() {
    if (DataMgr.gameParams.get(GameParams.energy_award) <= 0) {
      // MultiPlatform.Ins.platformJs.trackEvent(EventUMA.SHARE_ENERGY, {});
      DataMgr.gameParams.set(GameParams.energy_award, DataMgr.gameParams.get(GameParams.energy_award) + 1);
      EnergyModel.Ins().addEnergy(3);
      this.clickClose();
    }
  }

  onAd() {
    if (DataMgr.gameParams.get(GameParams.energy_award) > 0) {
      // MultiPlatform.Ins.platformJs.trackEvent(EventUMA.AD_ENERGY, {});
      DataMgr.gameParams.set(GameParams.energy_award, DataMgr.gameParams.get(GameParams.energy_award) + 1);
      // GravityModel.Ins().track(GEEvent.AD_SHOW_END, { adPosition: '增加体力' });
      switch (DataMgr.gameParams.get(GameParams.energy_award)) {
        case 2:
          EnergyModel.Ins().addEnergy(5);
          break;
        case 3:
          EventManager.Ins().emit(EventType.REFRESH_ENERGY);
          // EventManager.Ins().emit(EventType.UPDATE_ORDER_UI);
          break;
      }
      this.clickClose();
    }
  }

  /** 点击分享 */
  onClickShare() {
    if (DataMgr.gameParams.get(GameParams.energy_award) > 0) return;
    // EventManager.Ins().emit(EventType.CLICK_SOUND_2);
    MultiPlatform.Ins().platformUnit.shareEnergy(ShareEvent.Energy);
  }

  /** 点击视频 */
  onClickAd() {
    if (DataMgr.gameParams.get(GameParams.energy_award) <= 0) return;

    this.onAd();
    // EventManager.Ins().emit(EventType.CLICK_SOUND_2);
    // GravityModel.Ins().track(GEEvent.AD_SHOW, { adPosition: '增加体力' });
    // MultiPlatform.Ins().platformUnit.showVideo(VideoEvent.Energy);
  }
}
